package com.xzit.headline.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.xzit.headline.R;
import com.xzit.headline.adapter.MyNewPagerAdapter;
import com.xzit.headline.adapter.MyPagerAdapter;
import com.xzit.headline.bean.ResponseData;
import com.xzit.headline.fragment.sub.Page1Fragment;
import com.xzit.headline.fragment.sub.Page2Fragment;
import com.xzit.headline.fragment.sub.Page3Fragment;
import com.xzit.headline.fragment.sub.Page4Fragment;
import com.xzit.headline.net.MyApi;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class HomeFragment extends BaseFragment {
    private ViewPager viewPager;
    private TabLayout tabLayout;

    @Override
    public View getMyView() {
        //0.布局ViewPager和TabLayout

        //1.加载页面布局
        View view = View.inflate(getActivity(), R.layout.fragment_home, null);

        //2.初始化组件
        viewPager = view.findViewById(R.id.viewPager);
        //2.初始化指示器
        tabLayout = view.findViewById(R.id.tabLayout);

        listenerChanged();

        //弹出页面
        return view;
    }


    private void listenerChanged() {
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            //切换完成回调
            @Override
            public void onPageSelected(int position) {
                //根据当前页面显示有屏幕的编号,获取对应的数据
                ResponseData.DataBean dataBean = adapter.getDatas().get(position);
                Toast.makeText(getContext(), dataBean.title + "" + dataBean.url, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private MyNewPagerAdapter adapter;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //1.retrofit  当前是子类,继承父类BaseFragment

        //2.发请求,重点是发请求获取服务端的数据

        //将json的数据处理一下,使用插件gsonformat生成javabean
        retrofit.create(MyApi.class).getType().enqueue(new Callback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                //请求成功返回数据
                ResponseData data = response.body();
                System.out.println(data.data);
                System.out.println(data.retcode);

                //-------------
                adapter = new MyNewPagerAdapter(getFragmentManager(), data.data);

                //5.关联适配器
                viewPager.setAdapter(adapter);
                tabLayout.setupWithViewPager(viewPager);//-->viewPager-->adapter-->getPagerTitle
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                //请求失败返回数据
                System.out.println("失败---");
            }
        });
    }
}
